<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择商户类型" clearable>
          <el-option
            v-for="dict in dict.type.shop_restaurant_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="推荐指数" prop="recValue">
        <el-input
          v-model="queryParams.recValue"
          placeholder="请输入推荐指数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.shop_business_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shop:restaurant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:restaurant:edit']"
        >修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['shop:restaurant:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['shop:restaurant:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="restaurantList" highlight-current-row @current-change="handleSelectionChange">
      <el-table-column type="index" width="55" align="center" />
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="缩略图" align="center" prop="imgAbbrev" width="100">
        <template v-slot="scope">
          <image-preview :src="scope.row.imgAbbrev" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="商户名称" align="center" prop="name" />
      <el-table-column label="商户类型" align="center" prop="type">
        <template v-slot="scope">
          <dict-tag :options="dict.type.shop_restaurant_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="人均消费（元）" align="center" prop="price" />
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :options="dict.type.shop_business_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="服务电话" align="center" prop="phone" />
      <el-table-column label="推荐指数" align="center" prop="recValue" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="160">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:restaurant:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleMenuType(scope.row)"-->
<!--            v-hasPermi="['shop:goods:edit']"-->
<!--          >设置菜品类型</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-dish"-->
<!--            @click="handleMenu(scope.row)"-->
<!--            v-hasPermi="['shop:restMenu:list']"-->
<!--          >菜单管理</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:restaurant:remove']"
          >删除</el-button>
          <el-dropdown
            size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['shop:restaurant:edit']">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleMenu" icon="el-icon-dish"
                                v-hasPermi="['shop:restaurant:edit']">菜单管理</el-dropdown-item>
              <el-dropdown-item command="handleMenuType" icon="el-icon-edit"
                                v-hasPermi="['shop:restaurant:edit']">设置菜品类型</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改餐饮商户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="餐饮商户名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入餐饮商户名称" />
        </el-form-item>
        <el-form-item label="餐饮商户类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择餐饮商户类型">
            <el-option
              v-for="dict in dict.type.shop_restaurant_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预定须知" prop="notice">
          <el-input v-model="form.notice" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="餐饮商户地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入餐饮商户地址" />
        </el-form-item>
        <el-form-item label="坐标，经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入坐标，经度" />
        </el-form-item>
        <el-form-item label="坐标，纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入坐标，纬度" />
        </el-form-item>
        <el-form-item label="人均消费" prop="price">
          <el-input v-model="form.price" placeholder="请输入人均消费" />
        </el-form-item>
        <el-form-item label="亮点" prop="feature">
          <el-input v-model="form.feature" placeholder="请输入亮点" />
        </el-form-item>
        <el-form-item label="餐饮商户描述">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="服务电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入服务电话" />
        </el-form-item>
        <el-form-item label="推荐指数" prop="recValue">
          <el-input v-model="form.recValue" placeholder="请输入推荐指数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRestaurant, getRestaurant, delRestaurant, addRestaurant, updateRestaurant } from "@/api/shop/restaurant";

export default {
  name: "Restaurant",
  dicts: ['shop_restaurant_type', 'shop_business_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 餐饮商户表格数据
      restaurantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        price: null,
        status: null,
        phone: null,
        recValue: null,
        createBy: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询餐饮商户列表 */
    getList() {
      this.loading = true;
      listRestaurant(this.queryParams).then(response => {
        this.restaurantList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        notice: null,
        address: null,
        longitude: null,
        latitude: null,
        price: null,
        status: null,
        feature: null,
        content: null,
        phone: null,
        recValue: null,
        isDel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (selection === null) {
        this.single = true
      } else {
        this.ids = selection.id
        this.names = selection.name
        this.single = false
      }
      // this.ids = selection.map(item => item.id)
      // this.names = selection.map(item => item.name)
      // this.single = selection.length!==1
      // this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加餐饮商户";
      this.$tab.openPage("添加餐饮商户", '/shop/rest-add/index');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // const id = row.id || this.ids
      // getRestaurant(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改餐饮商户";
      // });
      const name = row.name || this.names
      const id = row.id || this.ids
      this.$tab.openPage("修改[" + name + "]餐饮商户", '/shop/rest-add/index', {restId: id});
    },
    /** 设置房间类型按钮操作 */
    handleMenuType(row) {
      const id = row.id;
      const name = row.name;
      this.$tab.openPage("[" + name + "]维护类型", '/shop/restaurant/menu/type/index/' + id);
    },
    /** 菜单管理按钮操作 */
    handleMenu(row) {
      const params = {restId: row.id};
      this.$tab.openPage("[" + row.name + "]菜单管理", '/shop/restaurant/menu', params);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRestaurant(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRestaurant(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.name || this.names;
      this.$modal.confirm('是否确认删除【' + names + '】餐饮商户？').then(function() {
        return delRestaurant(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleMenu":
          this.handleMenu(row);
          break;
        case "handleMenuType":
          this.handleMenuType(row);
          break;
        default:
          break;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/restaurant/export', {
        ...this.queryParams
      }, `restaurant_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
